home *** CD-ROM | disk | FTP | other *** search
/ PC Media 4 / PC MEDIA CD04.iso / share / prog / rzsz0717 / sz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-22  |  34.7 KB  |  1,613 lines

  1. #define VERSION "3.38 06-22-94"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*
  5.  **************************************************************************
  6.  *
  7.  * sz.c By Chuck Forsberg,  Omen Technology INC
  8.  *    Copyright 1994 Omen Technology Inc All Rights Reserved
  9.  * 
  10.  *********************************************************************
  11.  *********************************************************************
  12.  * 
  13.  *
  14.  *    This version implements numerous enhancements including ZMODEM
  15.  *    Run Length Encoding and variable length headers.  These
  16.  *    features were not funded by the original Telenet development
  17.  *    contract.
  18.  * 
  19.  * 
  20.  * This software may be freely used for educational (didactic
  21.  * only) purposes.  "Didactic" means it is used as a study item
  22.  * in a course teaching the workings of computer protocols.
  23.  * 
  24.  * This software may also be freely used to support file transfer
  25.  * operations to or from duly licensed Omen Technology products.
  26.  * This includes DSZ, GSZ, ZCOMM, Professional-YAM and PowerCom.
  27.  * Institutions desiring to use rz/sz this way should add the
  28.  * following to the sz compile line:    -DCOMPL
  29.  * Programs based on stolen or public domain ZMODEM materials are
  30.  * not included.  Use with other commercial or shareware programs
  31.  * (Crosstalk, Procomm, etc.) REQUIRES REGISTRATION.
  32.  * 
  33.  *
  34.  *  Any programs which incorporate part or all of this code must be
  35.  *  provided in source form with this notice intact except by
  36.  *  prior written permission from Omen Technology Incorporated.
  37.  *  This includes compiled executables of this program.
  38.  *
  39.  *   The .doc files and the file "mailer.rz" must also be included.
  40.  * 
  41.  * Use of this software for commercial or administrative purposes
  42.  * except when exclusively limited to interfacing Omen Technology
  43.  * products requires license payment of $20.00 US per user
  44.  * (less in quantity, see mailer.rz).  Use of this code by
  45.  * inclusion, decompilation, reverse engineering or any other means
  46.  * constitutes agreement to these conditions and acceptance of
  47.  * liability to license the materials and payment of reasonable
  48.  * legal costs necessary to enforce this license agreement.
  49.  *
  50.  *
  51.  *        Omen Technology Inc
  52.  *        Post Office Box 4681
  53.  *        Portland OR 97208
  54.  *
  55.  *    This code is made available in the hope it will be useful,
  56.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  57.  *    DAMAGES OF ANY KIND.
  58.  *
  59.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  60.  */
  61.  
  62. char *Copyrsz = "Copyright 1994 Omen Technology Inc All Rights Reserved";
  63.  
  64. char *substr();
  65.  
  66. #define LOGFILE "/tmp/szlog"
  67. #define LOGFILE2 "szlog"
  68. #include <stdio.h>
  69. #include <signal.h>
  70. #include <ctype.h>
  71. #include <errno.h>
  72. extern int errno;
  73. #define STATIC
  74.  
  75. #define PATHLEN 256
  76. #define OK 0
  77. #define FALSE 0
  78. #ifdef TRUE
  79. #undef TRUE
  80. #endif
  81. #define TRUE 1
  82. #define ERROR (-1)
  83. /* Ward Christensen / CP/M parameters - Don't change these! */
  84. #define ENQ 005
  85. #define CAN ('X'&037)
  86. #define XOFF ('s'&037)
  87. #define XON ('q'&037)
  88. #define SOH 1
  89. #define STX 2
  90. #define EOT 4
  91. #define ACK 6
  92. #define NAK 025
  93. #define CPMEOF 032
  94. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  95. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  96. #define TIMEOUT (-2)
  97. #define RCDO (-3)
  98. #define GCOUNT (-4)
  99. #define RETRYMAX 10
  100.  
  101.  
  102. #define HOWMANY 2
  103. STATIC int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  104. unsigned Baudrate = 9600;        /* Default, set by first mode() call */
  105. STATIC unsigned Txwindow;    /* Control the size of the transmitted window */
  106. STATIC unsigned Txwspac;    /* Spacing between zcrcq requests */
  107. STATIC unsigned Txwcnt;    /* Counter used to space ack requests */
  108. STATIC long Lrxpos;    /* Receiver's last reported offset */
  109. STATIC int errors;
  110. char endmsg[80] = {0};    /* Possible message to display on exit */
  111. char Zsendmask[33];    /* Additional control chars to mask */
  112.  
  113. #include "rbsb.c"    /* most of the system dependent stuff here */
  114.  
  115. #include "crctab.c"
  116.  
  117. STATIC int Filesleft;
  118. STATIC long Totalleft;
  119.  
  120. /*
  121.  * Attention string to be executed by receiver to interrupt streaming data
  122.  *  when an error is detected.  A pause (0336) may be needed before the
  123.  *  ^C (03) or after it.
  124.  */
  125. #ifdef READCHECK
  126. STATIC char Myattn[] = { 0 };
  127. #else
  128. #ifdef USG
  129. STATIC char Myattn[] = { 03, 0336, 0 };
  130. #endif
  131. #endif
  132.  
  133. FILE *in;
  134.  
  135. STATIC int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  136.  
  137. #ifdef TXBSIZE
  138. #define TXBMASK (TXBSIZE-1)
  139. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  140. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  141. #else
  142. STATIC char txbuf[1024];
  143. #endif
  144. STATIC long vpos = 0;        /* Number of bytes read from file */
  145.  
  146. STATIC char Lastrx;
  147. STATIC char Crcflg;
  148. STATIC int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  149. STATIC int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  150. STATIC int Fullname=0;        /* transmit full pathname */
  151. STATIC int Unlinkafter=0;    /* Unlink file after it is sent */
  152. STATIC int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  153. STATIC int firstsec;
  154. STATIC int errcnt=0;        /* number of files unreadable */
  155. STATIC int Skipbitch=0;
  156. STATIC int blklen=128;        /* length of transmitted records */
  157. STATIC int Optiong;        /* Let it rip no wait for sector ACK's */
  158. STATIC int Eofseen;        /* EOF seen on input set by zfilbuf */
  159. STATIC int BEofseen;        /* EOF seen on input set by fooseek */
  160. STATIC int Totsecs;        /* total number of sectors this file */
  161. STATIC int Filcnt=0;        /* count of number of files opened */
  162. STATIC unsigned Rxbuflen=16384;    /* Receiver's max buffer length */
  163. STATIC long Tframlen = 0;    /* Override for tx frame length */
  164. STATIC int blkopt=0;        /* Override value for zmodem blklen */
  165. STATIC int Rxflags = 0;
  166. STATIC long bytcnt;
  167. STATIC int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  168. STATIC char Lzconv;    /* Local ZMODEM file conversion request */
  169. STATIC char Lzmanag;    /* Local ZMODEM file management request */
  170. STATIC int Lskipnocor;
  171. STATIC char Lztrans;
  172. STATIC int Command;        /* Send a command, then exit. */
  173. STATIC char *Cmdstr;        /* Pointer to the command string */
  174. STATIC int Cmdack1;        /* Rx ACKs command, then do it */
  175. STATIC int Exitcode;
  176. STATIC int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  177.             /* 2= Character transparency test */
  178. STATIC char *qbf=
  179.  "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  180. STATIC long Lastsync;    /* Last offset to which we got a ZRPOS */
  181. STATIC int Beenhereb4;        /* How many times we've been ZRPOS'd here */
  182. STATIC int Ksendstr;        /* 1= Send esc-?-3-4-l to remote kermit */
  183. STATIC char *ksendbuf = "\033[?34l";
  184.  
  185. STATIC jmp_buf tohere;        /* For the interrupt on RX timeout */
  186. STATIC jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  187.  
  188.  
  189. /* called by signal interrupt or terminate to clean things up */
  190. void
  191. bibi(n)
  192. {
  193.     canit(); fflush(stdout); mode(0);
  194.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  195.     if (n == SIGQUIT)
  196.         abort();
  197.     if (n == 99)
  198.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  199.     exit(3);
  200. }
  201.  
  202. /* Called when ZMODEM gets an interrupt (^X) */
  203. void
  204. onintr(c)
  205. {
  206.     signal(SIGINT, SIG_IGN);
  207.     longjmp(intrjmp, -1);
  208. }
  209.  
  210. STATIC int Zctlesc;    /* Encode control characters */
  211. STATIC int Nozmodem = 0;    /* If invoked as "sb" */
  212. STATIC char *Progname = "sz";
  213. STATIC int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  214.  
  215. /*
  216.  * Log an error
  217.  */
  218. /*VARARGS1*/
  219. void
  220. zperr(s,p,u)
  221. char *s, *p, *u;
  222. {
  223.     if (Verbose <= 0)
  224.         return;
  225.     fprintf(stderr, "Retry %d: ", errors);
  226.     fprintf(stderr, s, p, u);
  227.     fprintf(stderr, "\n");
  228. }
  229.  
  230. #include "zm.c"
  231. #include "zmr.c"
  232.  
  233. main(argc, argv)
  234. char *argv[];
  235. {
  236.     register char *cp;
  237.     register npats;
  238.     char **patts;
  239.  
  240.     if ((cp = getenv("ZNULLS")) && *cp)
  241.         Znulls = atoi(cp);
  242.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  243.         Restricted=TRUE;
  244.     inittty();
  245.     chkinvok(argv[0]);
  246.  
  247.     Rxtimeout = 600;
  248.     npats=0;
  249.     if (argc<2)
  250.         usage();
  251.     while (--argc) {
  252.         cp = *++argv;
  253.         if (*cp++ == '-' && *cp) {
  254.             while ( *cp) {
  255.                 if (isdigit(*cp)) {
  256.                     ++cp;  continue;
  257.                 }
  258.                 switch(*cp++) {
  259.                 case '\\':
  260.                      *cp = toupper(*cp);  continue;
  261.                 case '+':
  262.                     Lzmanag = ZMAPND; break;
  263.                 case 'a':
  264.                     if (Nozmodem || Modem2)
  265.                         usage();
  266.                     Lzconv = ZCNL;  break;
  267.                 case 'b':
  268.                     Lzconv = ZCBIN; break;
  269.                 case 'c':
  270.                     Lzmanag = ZMCHNG;  break;
  271.                 case 'd':
  272.                     ++Dottoslash;
  273.                     /* **** FALL THROUGH TO **** */
  274.                 case 'f':
  275.                     Fullname=TRUE; break;
  276.                         case 'g' :
  277.                     Ksendstr = TRUE; break;
  278.                 case 'e':
  279.                     Zctlesc = 1; break;
  280.                 case 'k':
  281.                     blklen=1024; break;
  282.                 case 'L':
  283.                     if (isdigit(*cp))
  284.                         blkopt = atoi(cp);
  285.                     else {
  286.                         if (--argc < 1)
  287.                             usage();
  288.                         blkopt = atoi(*++argv);
  289.                     }
  290.                     if (blkopt<24 || blkopt>1024)
  291.                         usage();
  292.                     break;
  293.                 case 'l':
  294.                     if (isdigit(*cp))
  295.                         Tframlen = atol(cp);
  296.                     else {
  297.                         if (--argc < 1)
  298.                             usage();
  299.                         Tframlen = atol(*++argv);
  300.                     }
  301.                     if (Tframlen<32 || Tframlen>65535L)
  302.                         usage();
  303.                     break;
  304.                 case 'N':
  305.                     Lzmanag = ZMNEWL;  break;
  306.                 case 'n':
  307.                     Lzmanag = ZMNEW;  break;
  308.                 case 'o':
  309.                     Wantfcs32 = FALSE; break;
  310.                 case 'p':
  311.                     Lzmanag = ZMPROT;  break;
  312.                 case 'r':
  313.                     if (Lzconv == ZCRESUM)
  314.                         Lzmanag = (Lzmanag & ZMMASK) | ZMCRC;
  315.                     Lzconv = ZCRESUM; break;
  316.                 case 'T':
  317.                     chartest(1); chartest(2);
  318.                     mode(0);  exit(0);
  319.                 case 'u':
  320.                     ++Unlinkafter; break;
  321.                 case 'v':
  322.                     ++Verbose; break;
  323.                 case 'w':
  324.                     if (isdigit(*cp))
  325.                         Txwindow = atoi(cp);
  326.                     else {
  327.                         if (--argc < 1)
  328.                             usage();
  329.                         Txwindow = atoi(*++argv);
  330.                     }
  331.                     if (Txwindow < 256)
  332.                         Txwindow = 256;
  333.                     Txwindow = (Txwindow/64) * 64;
  334.                     Txwspac = Txwindow/4;
  335.                     if (blkopt > Txwspac
  336.                      || (!blkopt && Txwspac < 1024))
  337.                         blkopt = Txwspac;
  338.                     break;
  339.                 case 'x':
  340.                     Skipbitch = 1;  break;
  341.                 case 'Y':
  342.                     Lskipnocor = TRUE;
  343.                     /* **** FALLL THROUGH TO **** */
  344.                 case 'y':
  345.                     Lzmanag = ZMCLOB; break;
  346.                 case 'Z':
  347.                 case 'z':
  348.                     Lztrans = ZTRLE;  break;
  349.                 default:
  350.                     usage();
  351.                 }
  352.             }
  353.         }
  354.         else if (Command) {
  355.             if (argc != 1) {
  356.                 usage();
  357.             }
  358.             Cmdstr = *argv;
  359.         }
  360.         else if ( !npats && argc>0) {
  361.             if (argv[0][0]) {
  362.                 npats=argc;
  363.                 patts=argv;
  364.             }
  365.         }
  366.     }
  367.     if (npats < 1 && !Command && !Test) 
  368.         usage();
  369.     if (Verbose) {
  370.         if (freopen(LOGFILE, "a", stderr)==NULL)
  371.             if (freopen(LOGFILE2, "a", stderr)==NULL) {
  372.                 printf("Can't open log file!");
  373.                 exit(2);
  374.             }
  375.         setbuf(stderr, NULL);
  376.     }
  377.     vfile("%s %s for %s tty=%s\n", Progname, VERSION, OS, Nametty);
  378.  
  379.     mode(3);
  380.  
  381.     if (signal(SIGINT, bibi) == SIG_IGN) {
  382.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  383.     } else {
  384.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  385.     }
  386. #ifdef SIGQUIT
  387.     signal(SIGQUIT, SIG_IGN);
  388. #endif
  389. #ifdef SIGTERM
  390.     signal(SIGTERM, bibi);
  391. #endif
  392.  
  393.     countem(npats, patts);
  394.  
  395.     if (!Modem2 && !Nozmodem) {
  396.         if (Ksendstr)
  397.             printf(ksendbuf);
  398.         printf("rz\r");  fflush(stdout);
  399.         stohdr(0x80L);    /* Show we can var header */
  400.         if (Command)
  401.             Txhdr[ZF0] = ZCOMMAND;
  402.         zshhdr(4, ZRQINIT, Txhdr);
  403.     }
  404.     fflush(stdout);
  405.  
  406.  
  407.     if (Command) {
  408.         if (getzrxinit()) {
  409.             Exitcode=1; canit();
  410.         }
  411.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  412.             Exitcode=1; canit();
  413.         }
  414.     } else if (wcsend(npats, patts)==ERROR) {
  415.         Exitcode=1;
  416.         canit();
  417.     }
  418.     if (endmsg[0]) {
  419.         printf("\r\n%s: %s\r\n", Progname, endmsg);
  420.         if (Verbose)
  421.             fprintf(stderr, "%s\r\n", endmsg);
  422.     }
  423.     printf("%s %s finished.\r\n", Progname, VERSION);
  424.     fflush(stdout);
  425.     mode(0);
  426.     if(errcnt || Exitcode)
  427.         exit(1);
  428.  
  429. #ifndef REGISTERED
  430.     /* Removing or disabling this code without registering is theft */
  431.     if (!Usevhdrs)  {
  432.         printf("\n\n\nPlease read the License Agreement in sz.doc\n");
  433.         fflush(stdout);
  434.         sleep(10);
  435.     }
  436. #endif
  437.     exit(0);
  438.     /*NOTREACHED*/
  439. }
  440.  
  441. /* Say "bibi" to the receiver, try to do it cleanly */
  442. void
  443. saybibi()
  444. {
  445.     for (;;) {
  446.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  447.         zshhdr(4, ZFIN, Txhdr);    /*  to make debugging easier */
  448.         switch (zgethdr(Rxhdr)) {
  449.         case ZFIN:
  450.             sendline('O'); sendline('O'); flushmo();
  451.         case ZCAN:
  452.         case TIMEOUT:
  453.             return;
  454.         }
  455.     }
  456. }
  457.  
  458. wcsend(argc, argp)
  459. char *argp[];
  460. {
  461.     register n;
  462.  
  463.     Crcflg=FALSE;
  464.     firstsec=TRUE;
  465.     bytcnt = -1;
  466.     if (Nozmodem) {
  467.         printf("Start your local YMODEM receive.     ");
  468.         fflush(stdout);
  469.     }
  470.     for (n=0; n<argc; ++n) {
  471.         Totsecs = 0;
  472.         if (wcs(argp[n])==ERROR)
  473.             return ERROR;
  474.     }
  475.     Totsecs = 0;
  476.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  477.         if (!Nozmodem && !Modem2) {
  478.             Command = TRUE;
  479.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  480.             if (getnak()) {
  481.                 Exitcode=1; canit();
  482.             }
  483.             if (!Zmodem)
  484.                 canit();
  485.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  486.                 Exitcode=1; canit();
  487.             }
  488.             Exitcode = 1; return OK;
  489.         }
  490.         canit();
  491.         sprintf(endmsg, "Can't open any requested files");
  492.         return ERROR;
  493.     }
  494.     if (Zmodem)
  495.         saybibi();
  496.     else if ( !Modem2)
  497.         wctxpn("");
  498.     return OK;
  499. }
  500.  
  501. wcs(oname)
  502. char *oname;
  503. {
  504.     register c;
  505.     struct stat f;
  506.     char name[PATHLEN];
  507.  
  508.     strcpy(name, oname);
  509.  
  510.     if (Restricted) {
  511.         /* restrict pathnames to current tree or uucppublic */
  512.         if ( substr(name, "../")
  513.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  514.             canit();  sprintf(endmsg,"Security Violation");
  515.             return ERROR;
  516.         }
  517.     }
  518.  
  519.     in=fopen(oname, "r");
  520.  
  521.     if (in==NULL) {
  522.         ++errcnt;
  523.         return OK;    /* pass over it, there may be others */
  524.     }
  525.     BEofseen = Eofseen = 0;  vpos = 0;
  526.  
  527.     /* Check for directory */
  528.     fstat(fileno(in), &f);
  529. #ifdef POSIX
  530.     if (S_ISDIR(f.st_mode))
  531. #else
  532.     c = f.st_mode & S_IFMT;
  533.     if (c == S_IFDIR || c == S_IFBLK)
  534. #endif
  535.     {
  536.         fclose(in);
  537.         return OK;
  538.     }
  539.  
  540.     ++Filcnt;
  541.     switch (wctxpn(name)) {
  542.     case ZSKIP:
  543.     case ZFERR:
  544.         return OK;
  545.     case OK:
  546.         break;
  547.     default:
  548.         return ERROR;
  549.     }
  550.     if (!Zmodem && wctx(f.st_size))
  551.         return ERROR;
  552.  
  553.     if (Unlinkafter)
  554.         unlink(oname);
  555.  
  556.     return 0;
  557. }
  558.  
  559. /*
  560.  * generate and transmit pathname block consisting of
  561.  *  pathname (null terminated),
  562.  *  file length, mode time and file mode in octal
  563.  *  as provided by the Unix fstat call.
  564.  *  N.B.: modifies the passed name, may extend it!
  565.  */
  566. wctxpn(name)
  567. char *name;
  568. {
  569.     register char *p, *q;
  570.     char name2[PATHLEN];
  571.     struct stat f;
  572.  
  573.     if (Modem2) {
  574.         if (*name && fstat(fileno(in), &f)!= -1) {
  575.             fprintf(stderr, "Sending %s, %ld XMODEM blocks. ",
  576.               name, (127+f.st_size)>>7);
  577.         }
  578.         printf("Start your local XMODEM receive.     ");
  579.         fflush(stdout);
  580.         return OK;
  581.     }
  582.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  583.     if ( !Zmodem)
  584.         if (getnak())
  585.             return ERROR;
  586.  
  587.     q = (char *) 0;
  588.     if (Dottoslash) {        /* change . to . */
  589.         for (p=name; *p; ++p) {
  590.             if (*p == '/')
  591.                 q = p;
  592.             else if (*p == '.')
  593.                 *(q=p) = '/';
  594.         }
  595.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  596.             q += 8;            /*   make it .ext */
  597.             strcpy(name2, q);    /* save excess of name */
  598.             *q = '.';
  599.             strcpy(++q, name2);    /* add it back */
  600.         }
  601.     }
  602.  
  603.     for (p=name, q=txbuf ; *p; )
  604.         if ((*q++ = *p++) == '/' && !Fullname)
  605.             q = txbuf;
  606.     *q++ = 0;
  607.     p=q;
  608.     while (q < (txbuf + 1024))
  609.         *q++ = 0;
  610.     if (*name) {
  611.         if (fstat(fileno(in), &f)!= -1)
  612.             sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  613.               f.st_mode, Filesleft, Totalleft);
  614.         Totalleft -= f.st_size;
  615.     }
  616.     if (--Filesleft <= 0)
  617.         Filesleft = Totalleft = 0;
  618.     if (Totalleft < 0)
  619.         Totalleft = 0;
  620.  
  621.     /* force 1k blocks if name won't fit in 128 byte block */
  622.     if (txbuf[125])
  623.         blklen=1024;
  624.     else {        /* A little goodie for IMP/KMD */
  625.         txbuf[127] = (f.st_size + 127) >>7;
  626.         txbuf[126] = (f.st_size + 127) >>15;
  627.     }
  628.     if (Zmodem)
  629.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  630.     if (wcputsec(txbuf, 0, 128)==ERROR)
  631.         return ERROR;
  632.     return OK;
  633. }
  634.  
  635. getnak()
  636. {
  637.     register firstch;
  638.  
  639.     Lastrx = 0;
  640.     for (;;) {
  641.         switch (firstch = readline(800)) {
  642.         case ZPAD:
  643.             if (getzrxinit())
  644.                 return ERROR;
  645.             return FALSE;
  646.         case TIMEOUT:
  647.             sprintf(endmsg, "Timeout waiting for ZRINIT");
  648.             return TRUE;
  649.         case WANTG:
  650. #ifdef MODE2OK
  651.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  652. #endif
  653.             Optiong = TRUE;
  654.             blklen=1024;
  655.         case WANTCRC:
  656.             Crcflg = TRUE;
  657.         case NAK:
  658.             return FALSE;
  659.         case CAN:
  660.             if ((firstch = readline(20)) == CAN && Lastrx == CAN) {
  661.                 sprintf(endmsg, "Got CAN waiting to send file");
  662.                 return TRUE;
  663.             }
  664.         default:
  665.             break;
  666.         }
  667.         Lastrx = firstch;
  668.     }
  669. }
  670.  
  671.  
  672. wctx(flen)
  673. long flen;
  674. {
  675.     register int thisblklen;
  676.     register int sectnum, attempts, firstch;
  677.     long charssent;
  678.  
  679.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  680.     vfile("wctx:file length=%ld", flen);
  681.  
  682.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  683.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  684.         ;
  685.     if (firstch==CAN) {
  686.         zperr("Receiver CANcelled");
  687.         return ERROR;
  688.     }
  689.     if (firstch==WANTCRC)
  690.         Crcflg=TRUE;
  691.     if (firstch==WANTG)
  692.         Crcflg=TRUE;
  693.     sectnum=0;
  694.     for (;;) {
  695.         if (flen <= (charssent + 896L))
  696.             thisblklen = 128;
  697.         if ( !filbuf(txbuf, thisblklen))
  698.             break;
  699.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  700.             return ERROR;
  701.         charssent += thisblklen;
  702.     }
  703.     fclose(in);
  704.     attempts=0;
  705.     do {
  706.         purgeline();
  707.         sendline(EOT);
  708.         flushmo();
  709.         ++attempts;
  710.     }
  711.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  712.     if (attempts == RETRYMAX) {
  713.         zperr("No ACK on EOT");
  714.         return ERROR;
  715.     }
  716.     else
  717.         return OK;
  718. }
  719.  
  720. wcputsec(buf, sectnum, cseclen)
  721. char *buf;
  722. int sectnum;
  723. int cseclen;    /* data length of this sector to send */
  724. {
  725.     register checksum, wcj;
  726.     register char *cp;
  727.     unsigned oldcrc;
  728.     int firstch;
  729.     int attempts;
  730.  
  731.     firstch=0;    /* part of logic to detect CAN CAN */
  732.  
  733.     if (Verbose>2)
  734.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  735.     else if (Verbose>1)
  736.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  737.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  738.         Lastrx= firstch;
  739.         sendline(cseclen==1024?STX:SOH);
  740.         sendline(sectnum);
  741.         sendline(-sectnum -1);
  742.         oldcrc=checksum=0;
  743.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  744.             sendline(*cp);
  745.             oldcrc=updcrc((0377& *cp), oldcrc);
  746.             checksum += *cp++;
  747.         }
  748.         if (Crcflg) {
  749.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  750.             sendline((int)oldcrc>>8);
  751.             sendline((int)oldcrc);
  752.         }
  753.         else
  754.             sendline(checksum);
  755.         flushmo();
  756.  
  757.         if (Optiong) {
  758.             firstsec = FALSE; return OK;
  759.         }
  760.         firstch = readline(Rxtimeout);
  761. gotnak:
  762.         switch (firstch) {
  763.         case CAN:
  764.             if(Lastrx == CAN) {
  765. cancan:
  766.                 zperr("Cancelled");  return ERROR;
  767.             }
  768.             break;
  769.         case TIMEOUT:
  770.             zperr("Timeout on sector ACK"); continue;
  771.         case WANTCRC:
  772.             if (firstsec)
  773.                 Crcflg = TRUE;
  774.         case NAK:
  775.             zperr("NAK on sector"); continue;
  776.         case ACK: 
  777.             firstsec=FALSE;
  778.             Totsecs += (cseclen>>7);
  779.             return OK;
  780.         case ERROR:
  781.             zperr("Got burst for sector ACK"); break;
  782.         default:
  783.             zperr("Got %02x for sector ACK", firstch); break;
  784.         }
  785.         for (;;) {
  786.             Lastrx = firstch;
  787.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  788.                 break;
  789.             if (firstch == NAK || firstch == WANTCRC)
  790.                 goto gotnak;
  791.             if (firstch == CAN && Lastrx == CAN)
  792.                 goto cancan;
  793.         }
  794.     }
  795.     zperr("Retry Count Exceeded");
  796.     return ERROR;
  797. }
  798.  
  799. /* fill buf with count chars padding with ^Z for CPM */
  800. filbuf(buf, count)
  801. register char *buf;
  802. {
  803.     register m;
  804.  
  805.     m = read(fileno(in), buf, count);
  806.     if (m <= 0)
  807.         return 0;
  808.     while (m < count)
  809.         buf[m++] = 032;
  810.     return count;
  811. }
  812.  
  813. /* Fill buffer with blklen chars */
  814. zfilbuf()
  815. {
  816.     int n;
  817.  
  818. #ifdef TXBSIZE
  819.     vfile("zfilbuf: bytcnt =%lu vpos=%lu blklen=%d", bytcnt, vpos, blklen);
  820.     /* We assume request is within buffer, or just beyond */
  821.     txbuf = Txb + (bytcnt & TXBMASK);
  822.     if (vpos <= bytcnt) {
  823.         n = fread(txbuf, 1, blklen, in);
  824.  
  825.         vpos += n;
  826.         if (n < blklen)
  827.             Eofseen = 1;
  828.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  829.         return n;
  830.     }
  831.     if (vpos >= (bytcnt+blklen))
  832.         return blklen;
  833.     /* May be a short block if crash recovery etc. */
  834.     Eofseen = BEofseen;
  835.     return (vpos - bytcnt);
  836. #else
  837.     n = fread(txbuf, 1, blklen, in);
  838.     if (n < blklen) {
  839.         Eofseen = 1;
  840.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  841.     }
  842.     return n;
  843. #endif
  844. }
  845.  
  846. #ifdef TXBSIZE
  847. /* Replacement for brain damaged fseek function.  Returns 0==success */
  848. fooseek(fptr, pos, whence)
  849. FILE *fptr;
  850. long pos;
  851. {
  852.     long m, n;
  853.  
  854.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  855.     /* Seek offset < current buffer */
  856.     if (pos < (vpos -TXBSIZE +1024)) {
  857.         BEofseen = 0;
  858.         if (Canseek > 0) {
  859.             vpos = pos & ~TXBMASK;
  860.             if (vpos >= pos)
  861.                 vpos -= TXBSIZE;
  862.             if (fseek(fptr, vpos, 0))
  863.                 return 1;
  864.         }
  865.         else if (Canseek == 0) {
  866.             if (fseek(fptr, vpos = 0L, 0))
  867.                 return 1;
  868.         } else
  869.             return 1;
  870.         while (vpos < pos) {
  871.             n = fread(Txb, 1, TXBSIZE, fptr);
  872.             vpos += n;
  873.             vfile("n=%d vpos=%ld", n, vpos);
  874.             if (n < TXBSIZE) {
  875.                 BEofseen = 1;
  876.                 break;
  877.             }
  878.         }
  879.         vfile("vpos=%ld", vpos);
  880.         return 0;
  881.     }
  882.     /* Seek offset > current buffer (Crash Recovery, etc.) */
  883.     if (pos > vpos) {
  884.         if (Canseek)
  885.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  886.                 return 1;
  887.         while (vpos <= pos) {
  888.             txbuf = Txb + (vpos & TXBMASK);
  889.             m = TXBSIZE - (vpos & TXBMASK);
  890.             vfile("m=%ld vpos=%ld", m,vpos);
  891.                 n = fread(txbuf, 1, m, fptr);
  892.             vfile("n=%ld vpos=%ld", n,vpos);
  893.             vpos += n;
  894.             vfile("bo=%d m=%ld vpos=%ld", txbuf-Txb,m,vpos);
  895.             if (n < m) {
  896.                 BEofseen = 1;
  897.                 break;
  898.             }
  899.         }
  900.         return 0;
  901.     }
  902.     /* Seek offset is within current buffer */
  903.     vfile("within buffer: vpos=%ld", vpos);
  904.     return 0;
  905. }
  906. #define fseek fooseek
  907. #endif
  908.  
  909.  
  910. /*
  911.  * substr(string, token) searches for token in string s
  912.  * returns pointer to token within string if found, NULL otherwise
  913.  */
  914. char *
  915. substr(s, t)
  916. register char *s,*t;
  917. {
  918.     register char *ss,*tt;
  919.     /* search for first char of token */
  920.     for (ss=s; *s; s++)
  921.         if (*s == *t)
  922.             /* compare token with substring */
  923.             for (ss=s,tt=t; ;) {
  924.                 if (*tt == 0)
  925.                     return s;
  926.                 if (*ss++ != *tt++)
  927.                     break;
  928.             }
  929.     return NULL;
  930. }
  931.  
  932. char *usinfo[] = {
  933.     "Send Files and Commands with ZMODEM/YMODEM/XMODEM Protocol\n",
  934.     "Usage:    sz [-+abcdefgklLnNuvwxyYZ] [-] file ...",
  935.     "\t    zcommand [-egv] COMMAND",
  936.     "\t    zcommandi [-egv] COMMAND",
  937.     "\t    sb [-adfkuv] [-] file ...",
  938.     "\t    sx [-akuv] [-] file",
  939.     ""
  940. };
  941.  
  942. usage()
  943. {
  944.     char **pp;
  945.  
  946.     fprintf(stderr, "\n%s %s for %s by Chuck Forsberg, Omen Technology INC\n",
  947.      Progname, VERSION, OS);
  948.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  949.     for (pp=usinfo; **pp; ++pp)
  950.         fprintf(stderr, "%s\n", *pp);
  951.     fprintf(stderr,"\nCopyright (c) 1994 Omen Technology INC All Rights Reserved\n");
  952.     fprintf(stderr,
  953.      "See sz.doc for option descriptions and licensing information.\n\n");
  954.     fprintf(stderr,
  955.     "This program is designed to talk to terminal programs,\nnot to be called by one.\n");
  956.     fprintf(stderr,
  957.     "\nTechnical support hotline: 900-737-7836 (1-900-737-RTFM) $4.69/min.\n\n");
  958.     exit(3);
  959. }
  960.  
  961. /*
  962.  * Get the receiver's init parameters
  963.  */
  964. getzrxinit()
  965. {
  966.     register n;
  967.     struct stat f;
  968.  
  969.     for (n=10; --n>=0; ) {
  970.         
  971.         switch (zgethdr(Rxhdr)) {
  972.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  973.             stohdr(Rxpos);
  974.             zshhdr(4, ZACK, Txhdr);
  975.             continue;
  976.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  977.             stohdr(0L);
  978.             zshhdr(4, ZRQINIT, Txhdr);
  979.             continue;
  980.         case ZRINIT:
  981.             if (Rxhlen==4 && (Rxhdr[ZF1] & ZRQNVH)) {
  982.                 stohdr(0x80L);    /* Show we can var header */
  983.                 zshhdr(4, ZRQINIT, Txhdr);
  984.                 continue;
  985.             }
  986.             Rxflags = 0377 & Rxhdr[ZF0];
  987. #if COMPL
  988.             Usevhdrs = 1;
  989. #else
  990.             Usevhdrs = Rxhdr[ZF1] & CANVHDR;
  991. #endif
  992.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  993.             Zctlesc |= Rxflags & TESCCTL;
  994.             if (Rxhdr[ZF1] & ZRRQQQ)    /* Escape ctrls */
  995.                 initzsendmsk(Rxhdr+ZRPXQQ);
  996.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  997.             if ( !(Rxflags & CANFDX))
  998.                 Txwindow = 0;
  999.             vfile("Rxbuflen=%d Tframlen=%ld", Rxbuflen, Tframlen);
  1000.             signal(SIGINT, SIG_IGN);
  1001. #ifdef MODE2OK
  1002.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  1003. #endif
  1004.  
  1005. #ifndef READCHECK
  1006. #ifndef USG
  1007.             /* Use 1024 byte frames if no sample/interrupt */
  1008.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  1009.                 Rxbuflen = 1024;
  1010.                 vfile("Rxbuflen=%d", Rxbuflen);
  1011.             }
  1012. #endif
  1013. #endif
  1014.  
  1015.             /* Override to force shorter frame length */
  1016.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  1017.                 Rxbuflen = Tframlen;
  1018.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  1019.                 Rxbuflen = Tframlen;
  1020.             vfile("Rxbuflen=%d", Rxbuflen);
  1021.  
  1022.  
  1023.             /*
  1024.              * If input is not a regular file, force ACK's to
  1025.              *  prevent running beyond the buffer limits
  1026.              */
  1027.             if ( !Command) {
  1028.                 fstat(fileno(in), &f);
  1029.                 if (
  1030. #ifdef POSIX
  1031.                     !S_ISREG(f.st_mode)
  1032. #else
  1033.                     (f.st_mode & S_IFMT) != S_IFREG
  1034. #endif
  1035.                     ) {
  1036.                     Canseek = -1;
  1037. #ifdef TXBSIZE
  1038.                     Txwindow = TXBSIZE - 1024;
  1039.                     Txwspac = TXBSIZE/4;
  1040. #else
  1041.                     return ERROR;
  1042. #endif
  1043.                 }
  1044.             }
  1045.  
  1046.             /* Set initial subpacket length */
  1047.             if (blklen < 1024) {    /* Command line override? */
  1048.                 if (Baudrate > 300)
  1049.                     blklen = 256;
  1050.                 if (Baudrate > 1200)
  1051.                     blklen = 512;
  1052.                 if (Baudrate > 2400)
  1053.                     blklen = 1024;
  1054.             }
  1055.             if (Rxbuflen && blklen>Rxbuflen)
  1056.                 blklen = Rxbuflen;
  1057.             if (blkopt && blklen > blkopt)
  1058.                 blklen = blkopt;
  1059.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1060.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1061.  
  1062.  
  1063.             if (Lztrans == ZTRLE && (Rxflags & CANRLE))
  1064.                 Txfcs32 = 2;
  1065.             else
  1066.                 Lztrans = 0;
  1067.  
  1068.             return (sendzsinit());
  1069.         case ZCAN:
  1070.         case TIMEOUT:
  1071.             return ERROR;
  1072.         case ZRQINIT:
  1073.             if (Rxhdr[ZF0] == ZCOMMAND)
  1074.                 continue;
  1075.         default:
  1076.             zshhdr(4, ZNAK, Txhdr);
  1077.             continue;
  1078.         }
  1079.     }
  1080.     return ERROR;
  1081. }
  1082.  
  1083. /* Send send-init information */
  1084. sendzsinit()
  1085. {
  1086.     register c;
  1087.  
  1088.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1089.         return OK;
  1090.     errors = 0;
  1091.     for (;;) {
  1092.         stohdr(0L);
  1093. #ifdef ALTCANOFF
  1094.         Txhdr[ALTCOFF] = ALTCANOFF;
  1095. #endif
  1096.         if (Zctlesc) {
  1097.             Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);
  1098.         }
  1099.         else
  1100.             zsbhdr(4, ZSINIT, Txhdr);
  1101.         zsdata(Myattn, ZATTNLEN, ZCRCW);
  1102.         c = zgethdr(Rxhdr);
  1103.         switch (c) {
  1104.         case ZCAN:
  1105.             return ERROR;
  1106.         case ZACK:
  1107.             return OK;
  1108.         default:
  1109.             if (++errors > 19)
  1110.                 return ERROR;
  1111.             continue;
  1112.         }
  1113.     }
  1114. }
  1115.  
  1116. /* Send file name and related info */
  1117. zsendfile(buf, blen)
  1118. char *buf;
  1119. {
  1120.     register c;
  1121.     register unsigned long crc;
  1122.     long lastcrcrq = -1;
  1123.  
  1124.     for (errors=0; ++errors<11;) {
  1125.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1126.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1127.         if (Lskipnocor)
  1128.             Txhdr[ZF1] |= ZMSKNOLOC;
  1129.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1130.         Txhdr[ZF3] = 0;
  1131.         zsbhdr(4, ZFILE, Txhdr);
  1132.         zsdata(buf, blen, ZCRCW);
  1133. again:
  1134.         c = zgethdr(Rxhdr);
  1135.         switch (c) {
  1136.         case ZRINIT:
  1137.             while ((c = readline(50)) > 0)
  1138.                 if (c == ZPAD) {
  1139.                     goto again;
  1140.                 }
  1141.             continue;
  1142.         case ZCAN:
  1143.         case TIMEOUT:
  1144.         case ZABORT:
  1145.         case ZFIN:
  1146.             sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);
  1147.             return ERROR;
  1148.         default:
  1149.             sprintf(endmsg, "Got %d frame type on pathname", c);
  1150.             continue;
  1151.         case ERROR:
  1152.         case ZNAK:
  1153.             continue;
  1154.         case ZCRC:
  1155.             if (Rxpos != lastcrcrq) {
  1156.                 lastcrcrq = Rxpos;
  1157.                 crc = 0xFFFFFFFFL;
  1158.                 if (Canseek >= 0) {
  1159.                     fseek(in, 0L, 0);
  1160.                     while (((c = getc(in)) != EOF) && --lastcrcrq)
  1161.                         crc = UPDC32(c, crc);
  1162.                     crc = ~crc;
  1163.                     clearerr(in);    /* Clear possible EOF */
  1164.                     lastcrcrq = Rxpos;
  1165.                 }
  1166.             }
  1167.             stohdr(crc);
  1168.             zsbhdr(4, ZCRC, Txhdr);
  1169.             goto again;
  1170.         case ZFERR:
  1171.         case ZSKIP:
  1172.             if (Skipbitch)
  1173.                 ++errcnt;
  1174.             sprintf(endmsg, "File skipped by receiver request");
  1175.             fclose(in); return c;
  1176.         case ZRPOS:
  1177.             /*
  1178.              * Suppress zcrcw request otherwise triggered by
  1179.              * lastyunc==bytcnt
  1180.              */
  1181.             if (fseek(in, Rxpos, 0))
  1182.                 return ERROR;
  1183.             Lastsync = (bytcnt = Txpos = Lrxpos = Rxpos) -1;
  1184.             return zsendfdata();
  1185.         }
  1186.     }
  1187.     fclose(in); return ERROR;
  1188. }
  1189.  
  1190. /* Send the data in the file */
  1191. zsendfdata()
  1192. {
  1193.     register c, e, n;
  1194.     register newcnt;
  1195.     register long tcount = 0;
  1196.     int junkcount;        /* Counts garbage chars received by TX */
  1197.     static int tleft = 6;    /* Counter for test mode */
  1198.  
  1199.     junkcount = 0;
  1200.     Beenhereb4 = FALSE;
  1201. somemore:
  1202.     if (setjmp(intrjmp)) {
  1203. waitack:
  1204.         junkcount = 0;
  1205.         c = getinsync(0);
  1206. gotack:
  1207.         switch (c) {
  1208.         default:
  1209.         case ZCAN:
  1210.             fclose(in);
  1211.             return ERROR;
  1212.         case ZRINIT:
  1213.             fclose(in);
  1214.             return ZSKIP;
  1215.         case ZSKIP:
  1216.             if (Skipbitch)
  1217.                 ++errcnt;
  1218.             fclose(in);
  1219.             return c;
  1220.         case ZACK:
  1221.         case ZRPOS:
  1222.             break;
  1223.         }
  1224. #ifdef READCHECK
  1225.         /*
  1226.          * If the reverse channel can be tested for data,
  1227.          *  this logic may be used to detect error packets
  1228.          *  sent by the receiver, in place of setjmp/longjmp
  1229.          *  rdchk(Tty) returns non 0 if a character is available
  1230.          */
  1231.         while (rdchk(Tty)) {
  1232. #ifdef EATSIT
  1233.             switch (checked)
  1234. #else
  1235.             switch (readline(1))
  1236. #endif
  1237.             {
  1238.             case CAN:
  1239.             case ZPAD:
  1240.                 c = getinsync(1);
  1241.                 goto gotack;
  1242.             case XOFF:        /* Wait a while for an XON */
  1243.                 readline(100);
  1244.             }
  1245.         }
  1246. #endif
  1247.     }
  1248.  
  1249.     signal(SIGINT, onintr);
  1250.     newcnt = Rxbuflen;
  1251.     Txwcnt = 0;
  1252.     stohdr(Txpos);
  1253.     zsbhdr(4, ZDATA, Txhdr);
  1254.  
  1255.     /*
  1256.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1257.      *  many times.  Each time the signal should be caught, causing the
  1258.      *  file to be started over from the beginning.
  1259.      */
  1260.     if (Test) {
  1261.         if ( --tleft)
  1262.             while (tcount < 20000) {
  1263.                 printf(qbf); fflush(stdout);
  1264.                 tcount += strlen(qbf);
  1265. #ifdef READCHECK
  1266.                 while (rdchk(Tty)) {
  1267. #ifdef EATSIT
  1268.                     switch (checked)
  1269. #else
  1270.                     switch (readline(1))
  1271. #endif
  1272.                     {
  1273.                     case CAN:
  1274.                     case ZPAD:
  1275.                         goto waitack;
  1276.                     case XOFF:    /* Wait for XON */
  1277.                         readline(100);
  1278.                     }
  1279.                 }
  1280. #endif
  1281.             }
  1282.         signal(SIGINT, SIG_IGN); canit();
  1283.         sleep(3); purgeline(); mode(0);
  1284.         printf("\nsz: Tcount = %ld\n", tcount);
  1285.         if (tleft) {
  1286.             printf("ERROR: Interrupts Not Caught\n");
  1287.             exit(1);
  1288.         }
  1289.         exit(0);
  1290.     }
  1291.  
  1292.     do {
  1293.         n = zfilbuf();
  1294.         if (Eofseen)
  1295.             e = ZCRCE;
  1296.         else if (junkcount > 3)
  1297.             e = ZCRCW;
  1298.         else if (bytcnt == Lastsync)
  1299.             e = ZCRCW;
  1300.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1301.             e = ZCRCW;
  1302.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1303.             Txwcnt = 0;  e = ZCRCQ;
  1304.         } else
  1305.             e = ZCRCG;
  1306.         if (Verbose>1)
  1307.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1308.               Txpos, Crc32t?" CRC-32":"");
  1309.         zsdata(txbuf, n, e);
  1310.         bytcnt = Txpos += n;
  1311.         if (e == ZCRCW)
  1312.             goto waitack;
  1313. #ifdef READCHECK
  1314.         /*
  1315.          * If the reverse channel can be tested for data,
  1316.          *  this logic may be used to detect error packets
  1317.          *  sent by the receiver, in place of setjmp/longjmp
  1318.          *  rdchk(Tty) returns non 0 if a character is available
  1319.          */
  1320.         fflush(stdout);
  1321.         while (rdchk(Tty)) {
  1322. #ifdef EATSIT
  1323.             switch (checked)
  1324. #else
  1325.             switch (readline(1))
  1326. #endif
  1327.             {
  1328.             case CAN:
  1329.             case ZPAD:
  1330.                 c = getinsync(1);
  1331.                 if (c == ZACK)
  1332.                     break;
  1333.                 /* zcrce - dinna wanna starta ping-pong game */
  1334.                 zsdata(txbuf, 0, ZCRCE);
  1335.                 goto gotack;
  1336.             case XOFF:        /* Wait a while for an XON */
  1337.                 readline(100);
  1338.             default:
  1339.                 ++junkcount;
  1340.             }
  1341.         }
  1342. #endif    /* READCHECK */
  1343.         if (Txwindow) {
  1344.             while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {
  1345.                 vfile("%ld window >= %u", tcount, Txwindow);
  1346.                 if (e != ZCRCQ)
  1347.                     zsdata(txbuf, 0, e = ZCRCQ);
  1348.                 c = getinsync(1);
  1349.                 if (c != ZACK) {
  1350.                     zsdata(txbuf, 0, ZCRCE);
  1351.                     goto gotack;
  1352.                 }
  1353.             }
  1354.             vfile("window = %ld", tcount);
  1355.         }
  1356.     } while (!Eofseen);
  1357.     signal(SIGINT, SIG_IGN);
  1358.  
  1359.     for (;;) {
  1360.         stohdr(Txpos);
  1361.         zsbhdr(4, ZEOF, Txhdr);
  1362. egotack:
  1363.         switch (getinsync(0)) {
  1364.         case ZACK:
  1365.             goto egotack;
  1366.         case ZNAK:
  1367.             continue;
  1368.         case ZRPOS:
  1369.             goto somemore;
  1370.         case ZRINIT:
  1371.             fclose(in);
  1372.             return OK;
  1373.         case ZSKIP:
  1374.             if (Skipbitch)
  1375.                 ++errcnt;
  1376.             fclose(in);
  1377.             sprintf(endmsg, "File skipped by receiver request");
  1378.             return c;
  1379.         default:
  1380.             sprintf(endmsg, "Got %d trying to send end of file", c);
  1381.         case ERROR:
  1382.             fclose(in);
  1383.             return ERROR;
  1384.         }
  1385.     }
  1386. }
  1387.  
  1388. /*
  1389.  * Respond to receiver's complaint, get back in sync with receiver
  1390.  */
  1391. getinsync(flag)
  1392. {
  1393.     register c;
  1394.  
  1395.     for (;;) {
  1396.         if (Test) {
  1397.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1398.             Rxpos = 0; c = ZRPOS;
  1399.         } else
  1400.             c = zgethdr(Rxhdr);
  1401.         switch (c) {
  1402.         case ZCAN:
  1403.         case ZABORT:
  1404.         case ZFIN:
  1405.         case TIMEOUT:
  1406.             sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);
  1407.             return ERROR;
  1408.         case ZRPOS:
  1409.             if (Rxpos > bytcnt) {
  1410.                 sprintf(endmsg, "Nonstandard Protocol");
  1411.                 return ZRPOS;
  1412.             }
  1413.             /* ************************************* */
  1414.             /*  If sending to a buffered modem, you  */
  1415.             /*   might send a break at this point to */
  1416.             /*   dump the modem's buffer.         */
  1417.             clearerr(in);    /* In case file EOF seen */
  1418.             if (fseek(in, Rxpos, 0)) {
  1419.                 sprintf(endmsg, "Bad Seek");
  1420.                 return ERROR;
  1421.             }
  1422.             Eofseen = 0;
  1423.             bytcnt = Lrxpos = Txpos = Rxpos;
  1424.             if (Lastsync == Rxpos) {
  1425.                 if (++Beenhereb4 > 12) {
  1426.                     sprintf(endmsg, "Can't send block");
  1427.                     return ERROR;
  1428.                 }
  1429.                 if (Beenhereb4 > 4)
  1430.                     if (blklen > 32)
  1431.                         blklen /= 2;
  1432.             }
  1433.             Lastsync = Rxpos;
  1434.             return c;
  1435.         case ZACK:
  1436.             Lrxpos = Rxpos;
  1437.             if (flag || Txpos == Rxpos)
  1438.                 return ZACK;
  1439.             continue;
  1440.         case ZRINIT:
  1441.             return c;
  1442.         case ZSKIP:
  1443.             if (Skipbitch)
  1444.                 ++errcnt;
  1445.             sprintf(endmsg, "File skipped by receiver request");
  1446.             return c;
  1447.         case ERROR:
  1448.         default:
  1449.             zsbhdr(4, ZNAK, Txhdr);
  1450.             continue;
  1451.         }
  1452.     }
  1453. }
  1454.  
  1455.  
  1456. /* Send command and related info */
  1457. zsendcmd(buf, blen)
  1458. char *buf;
  1459. {
  1460.     register c;
  1461.     long cmdnum;
  1462.  
  1463.     cmdnum = getpid();
  1464.     errors = 0;
  1465.     for (;;) {
  1466.         stohdr(cmdnum);
  1467.         Txhdr[ZF0] = Cmdack1;
  1468.         zsbhdr(4, ZCOMMAND, Txhdr);
  1469.         zsdata(buf, blen, ZCRCW);
  1470. listen:
  1471.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1472.         Usevhdrs = 0;        /* Allow rx to send fixed len headers */
  1473.         c = zgethdr(Rxhdr);
  1474.  
  1475.         switch (c) {
  1476.         case ZRINIT:
  1477.             goto listen;    /* CAF 8-21-87 */
  1478.         case ERROR:
  1479.         case GCOUNT:
  1480.         case TIMEOUT:
  1481.             if (++errors > 11)
  1482.                 return ERROR;
  1483.             continue;
  1484.         case ZCAN:
  1485.         case ZABORT:
  1486.         case ZFIN:
  1487.         case ZSKIP:
  1488.         case ZRPOS:
  1489.             return ERROR;
  1490.         default:
  1491.             if (++errors > 20)
  1492.                 return ERROR;
  1493.             continue;
  1494.         case ZCOMPL:
  1495.             Exitcode = Rxpos;
  1496.             saybibi();
  1497.             return OK;
  1498.         case ZRQINIT:
  1499.             vfile("******** RZ *******");
  1500.             system("rz");
  1501.             vfile("******** SZ *******");
  1502.             goto listen;
  1503.         }
  1504.     }
  1505. }
  1506.  
  1507. /*
  1508.  * If called as sb use YMODEM protocol
  1509.  */
  1510. chkinvok(s)
  1511. char *s;
  1512. {
  1513.     register char *p;
  1514.  
  1515.     p = s;
  1516.     while (*p == '-')
  1517.         s = ++p;
  1518.     while (*p)
  1519.         if (*p++ == '/')
  1520.             s = p;
  1521.     if (*s == 'v') {
  1522.         Verbose=1; ++s;
  1523.     }
  1524.     Progname = s;
  1525.     if (s[0]=='z' && s[1] == 'c') {
  1526.         Command = TRUE;
  1527.         if (s[8] == 'i')
  1528.             Cmdack1 = ZCACK1;
  1529.     }
  1530.     if (s[0]=='s' && s[1]=='b') {
  1531.         Nozmodem = TRUE; blklen=1024;
  1532.     }
  1533.     if (s[0]=='s' && s[1]=='x') {
  1534.         Modem2 = TRUE;
  1535.     }
  1536. }
  1537.  
  1538. countem(argc, argv)
  1539. register char **argv;
  1540. {
  1541.     struct stat f;
  1542.  
  1543.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1544.         f.st_size = -1;
  1545.         if (Verbose>2) {
  1546.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1547.             fflush(stderr);
  1548.         }
  1549.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1550.             ++Filesleft;  Totalleft += f.st_size;
  1551.         }
  1552.         if (Verbose>2)
  1553.             fprintf(stderr, " %ld", f.st_size);
  1554.     }
  1555.     if (Verbose>2)
  1556.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1557.           Filesleft, Totalleft);
  1558. }
  1559.  
  1560. chartest(m)
  1561. {
  1562.     register n;
  1563.  
  1564.     mode(m);
  1565.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1566.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1567.     printf("Hit Enter.\021");  fflush(stdout);
  1568.     readline(500);
  1569.  
  1570.     for (n = 0; n < 256; ++n) {
  1571.         if (!(n%8))
  1572.             printf("\r\n");
  1573.         printf("%02x ", n);  fflush(stdout);
  1574.         sendline(n);    flushmo();
  1575.         printf("  ");  fflush(stdout);
  1576.         if (n == 127) {
  1577.             printf("Hit Enter.\021");  fflush(stdout);
  1578.             readline(500);
  1579.             printf("\r\n");  fflush(stdout);
  1580.         }
  1581.     }
  1582.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1583.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1584.  
  1585.     while (n != TIMEOUT && n != ' ') {
  1586.         n = readline(400);
  1587.         printf("%02x\r\n", n);
  1588.         fflush(stdout);
  1589.     }
  1590.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1591.     fflush(stdout);
  1592. }
  1593.  
  1594. /*
  1595.  * Set additional control chars to mask in Zsendmask
  1596.  * according to bit array stored in char array at p
  1597.  */
  1598. initzsendmsk(p)
  1599. register char *p;
  1600. {
  1601.     register c;
  1602.  
  1603.     for (c = 0; c < 33; ++c) {
  1604.         if (p[c>>3] & (1 << (c & 7))) {
  1605.             Zsendmask[c] = 1;
  1606.             vfile("Escaping %02o", c);
  1607.         }
  1608.     }
  1609. }
  1610.  
  1611.  
  1612. /* End of sz.c */
  1613.